﻿using System;
using System.Web.UI.WebControls;

public partial class AdminCP_Reportes : System.Web.UI.Page
{
    CheckBox[] checkBoxes;
    private int numCheckBoxes = 9;

    protected void Page_Load(object sender, EventArgs e)
    {
        SessionValidation();
        
        string formChoices = Request.QueryString["formChoices"];
        string folio = Request.QueryString["folio"];
        
        LoadCheckBoxes();

        if (!IsPostBack && !String.IsNullOrEmpty(formChoices))
        {
            CheckValuesFromString(formChoices);
        } 
        if (!IsPostBack && !String.IsNullOrEmpty(folio))
        {
            LoadFolioTextBox(folio);
        }
        if (string.IsNullOrEmpty(folio) || string.IsNullOrEmpty(formChoices))
        {
            myPDF.Visible = false;
            iFrameDiv.Visible = true;
            FolioInfo.InnerText= "Ingrese el folio del cual desea generar el reporte:";
            goto BypassDivError;
        }

        DatabaseConnection dbCon = new DatabaseConnection();
        if(dbCon.FolioExist(Convert.ToInt32(folio)))
        {
            myPDF.Visible = true;
            iFrameDiv.Visible = true;
        } else
        {
            ErrorFolioDiv.Visible = true;
        }

        BypassDivError:
        string pdfPath = this.ResolveUrl(string.Format("~/Services/Reportes.ashx?formChoices={0}&folio={1}", formChoices, folio));
        myPDF.Attributes.Add("src", pdfPath);
    }


    // Valida que la sesión sea del administrador
    protected void SessionValidation()
    {
        String SessionID = (String)Session["SessionID"];
        String UserType = (String)Session["UserType"];

        if (String.IsNullOrEmpty(SessionID) || String.IsNullOrEmpty(UserType))
        {
            Session.RemoveAll();
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            if (!String.Equals(UserType, "Administradores"))
            {
                Session.RemoveAll();
                Response.Redirect("~/Default.aspx");
            }
        }
    }


    protected void LoadCheckBoxes()
    {
        checkBoxes = new CheckBox[numCheckBoxes];
        checkBoxes[0] = Form1CheckBox;
        checkBoxes[1] = Form2CheckBox;
        checkBoxes[2] = Form3CheckBox;
        checkBoxes[3] = Form4CheckBox;
        checkBoxes[4] = Form5CheckBox;
        checkBoxes[5] = Form6CheckBox;
        checkBoxes[6] = Form7CheckBox;
        checkBoxes[7] = Form8CheckBox;
        checkBoxes[8] = Form9CheckBox;
    }

    protected void GenerarReporteButton_Click(object sender, EventArgs e)
    {
        string formChoice = GetSerializedFormChoices();
        Response.Redirect("~/AdminCP/Reportes.aspx?formChoices=" + formChoice + "&folio=" + FolioTextBox.Text);
    }

    protected void LoadFolioTextBox(string folio)
    {
        FolioTextBox.Text = folio;
    }

    protected void CheckValuesFromString(string stringFormChoices)
    {
        for (int i = 0; i < numCheckBoxes; ++i)
        {
            checkBoxes[i].Checked = stringFormChoices[i].Equals('1');            
        }
    }

    protected string GetSerializedFormChoices()
    {
        string formChoices = "";

        for (int i = 0; i < 9; ++i)
        {
            if (checkBoxes[i].Checked)
            {
                formChoices += '1';
            }
            else
            {
                formChoices += '0';
            }
        }

        return formChoices;
    }
}